package sample;

import common.Constant;
import lombok.Data;
import annotation.ClassXpath;
import bean.FetchParams;
import annotation.FieldXpath;
import spider.Spider;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *
 * @author chen_
 * @date 2017/5/5
 */
public class DoulieSpider {
    public static Spider spider = new Spider();

    private static LinkedBlockingQueue<String>  fetchAddr() {
        LinkedBlockingQueue<String> q = new LinkedBlockingQueue<>(Constant.CAPACITY);
        for (int i = 0; i < 3; i++) {
            String url = "https://www.douban.com/doulist/982787/?start=" + 25 * i + "&sort=seq&sub_type=";
            q.offer(url);
            System.out.println(url);
        }
        return q;
    }

    @Data
    @ClassXpath(value = "//*[@class=doulist-subject]")
    public static class DouliePage{
        @FieldXpath(value = "*[@class=post]/a/@href")
        private String url;
        @FieldXpath(value = "*[@class=title]/a/text()")
        private String title;
        @FieldXpath(value = "*[@class=rating_nums]/text()")
        private String rating;
    }

    public static void main(String[] args) {
        spider.setQueues0(fetchAddr());
        spider.chain(
                url -> FetchParams.builder().url((String) url).clazz(DouliePage.class).build()
        ).print().end();
    }
}